package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.util.InputMethodUtils;
import com.house.rent.App;
import com.house.rent.R;
import com.house.rent.entity.UserEntity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author neo.duan
 * @date 2019-09-09 22:58
 * @desc 登录页
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.edit_phone)
    EditText mEdtPhone;
    @BindView(R.id.edit_pwd)
    EditText mEdtPwd;
    @BindView(R.id.rg_login)
    RadioGroup mRadioGroup;

    private boolean mIsAdmin;

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initTop() {
        enableBack(false);
        setTitle("登录");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initListeners() {
        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_check_user:
                    mIsAdmin = false;
                    break;
                case R.id.rb_check_admin:
                    mIsAdmin = true;
                    break;
                default:
                    mIsAdmin = false;
                    break;
            }
        });
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.btn_login, R.id.btn_register, R.id.tv_login_title})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_title:
                ApiActivity.start(mContext);
                break;
            case R.id.btn_login:
                InputMethodUtils.hideInputMethod(this);
                requestLogin();
                break;
            case R.id.btn_register:
                InputMethodUtils.hideInputMethod(this);
                RegisterActivity.start(mContext);
                break;
            default:
                break;
        }
    }

    private void requestLogin() {
        String phone = getPhone();
        if (TextUtils.isEmpty(phone)) {
            showMessage("请输入账号");
            return;
        }
        String pwd = getPwd();
        if (TextUtils.isEmpty(pwd)) {
            showMessage("请输入密码");
            return;
        }

        showLoading("正在登录...");
        addParams("mobile", phone);
        addParams("password", pwd);
        sendRequest("user/login", UserEntity.class);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof UserEntity) {
            showMessage("登录成功");

            //保存用户信息
            App.getInstance().saveUserEntity((UserEntity) obj);

            MainActivity.start(mContext);
        }
    }

    public String getPhone() {
        return mEdtPhone.getText().toString().trim();
    }

    public String getPwd() {
        return mEdtPwd.getText().toString().trim();
    }
}
